from django.contrib import admin
from booktest.models import HeroInfo, BookInfo
from django.contrib import admin
# Register your models here.


class HeroInfoStackInline(admin.StackedInline):
    """在一对多的关系中，可以在一端的编辑页面中编辑多端的对象，
    嵌入多端对象的方式包括表格、块两种。
    """
    model = HeroInfo  # 要g关联展示的模型
    extra = 1  # 额外的编辑框


class HeroInfoTabularInline(admin.TabularInline):
    """以表格状显示"""
    model = HeroInfo
    extra = 1


@admin.register(BookInfo)
class BookInfoAdmin(admin.ModelAdmin):
    """BookInfo模型管理类：管理它在站点的显示信息"""
    list_per_page = 2  # 每页显示多少条数据
    # actions_on_bottom = True  # 执行选项框在底部显示
    # actions_on_top = False
    # 控制列表页面展示的列数据 可以是字段 也可以是方法
    list_display = ["id", "btitle", "bread", "bpub_data", "pub_date_format", "bcomment", "is_delete"]
    ordering = ['id']
    # # 显示字段  默认全部展示
    # fields = ["btitle", "bpub_data"]
    # 分组显示字段 里面是元祖, 一个元祖代表一组，元祖里面包含字典
    fieldsets = [
        ["基本", {"fields": ["btitle", "bpub_data", "image"]}],
        ["高级", {
            "fields": ["bread", "bcomment"],
            "classes": ["collapse"]}]  # 是否折叠显示
    ]

    # inlines = [HeroInfoStackInline]
    inlines = [HeroInfoTabularInline]


class HeroInfoAdmin(admin.ModelAdmin):
    """控制HeroInfo"""
    list_per_page = 10
    list_display = ["id", "hname", "hcomment", "hbook", "is_delete", "book_read"]
    # 过滤数据 一般用于 有重复数据的字段
    list_filter = ["hbook"]
    # 搜索框
    search_fields = ["hname", "hcomment"]

    ordering = ['id']
    # 降序
    # ordering = ['-id']
# 把模型注册到admin站点  这样就可以在站点看到模型了
# admin.site.register(BookInfo)
admin.site.register(HeroInfo, HeroInfoAdmin)

admin.site.site_header = '传智书城'  # 设置网站页头
admin.site.site_title = '传智书城MIS'  # 设置页面标题
admin.site.index_title = '欢迎使用传智书城MIS'  # 设置首页标语
